package com.zhglxt.oa.activitiListeners.taskListener;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * @ClassName BusinessTripTaskListener
 * @Description 流程任务监听类，实现动态设置审批人
 * @Author moyongjun
 * @Date 2022/5/21 09:18
 * @Version 1.0
 **/
@Component("businessTripTaskListener")
public class BusinessTripTaskListener implements TaskListener, Serializable {
    /**
     * 任务监听的字段，xml中、可视化编辑器中设置
     */
    private Expression express;

    public Expression getExpression() {
        return express;
    }

    public void setExpression(Expression express) {
        this.express = express;
    }


    @Autowired
    private RuntimeService runtimeService;

    private final Logger logger= LoggerFactory.getLogger(BusinessTripTaskListener.class);
    // 监听到任务节点的下一个节点

    @Override
    public void notify(DelegateTask delegateTask) {

        // logger查看以下DelegateTask的信息
        logger.info("流程委派任务对象DelegateTask：{}",delegateTask);
        VariableScope variableScope=delegateTask;
        Object value = express.getValue(variableScope);
        logger.info("任务监听器获取到的表达式值是：{}",value);


        Date createTime = delegateTask.getCreateTime();
        logger.info("流程监听类中流程委派任务对象DelegateTask得到的参数：createTime:{}",createTime);
        String assignee = delegateTask.getAssignee();
        logger.info("流程监听类中流程委派任务对象DelegateTask得到的参数：assignee:{}",assignee);
        String processDefinitionId = delegateTask.getProcessDefinitionId();
        logger.info("流程监听类中流程委派任务对象DelegateTask得到的参数：processDefinitionId:{}",processDefinitionId);
        String processInstanceId = delegateTask.getProcessInstanceId();
        logger.info("流程监听类中流程委派任务对象DelegateTask得到的参数：processInstanceId:{}",processInstanceId);
        String taskDefinitionKey = delegateTask.getTaskDefinitionKey();
        logger.info("流程监听类中流程委派任务对象DelegateTask得到的参数：taskDefinitionKey:{}",taskDefinitionKey);
        String id = delegateTask.getId();
        logger.info("流程监听类中流程委派任务对象DelegateTask得到的参数：id:{}",id);
        String formKey = delegateTask.getFormKey();
        logger.info("流程监听类中流程委派任务对象DelegateTask得到的参数：formKey:{}",formKey);
        String eventName = delegateTask.getEventName();
        logger.info("流程监听类中流程委派任务对象DelegateTask得到的参数：eventName:{}",eventName);

        String owner = delegateTask.getOwner();
        logger.info("流程监听类中流程委派任务对象DelegateTask得到的参数：owner:{}",owner);
        DelegateExecution execution = delegateTask.getExecution();
        logger.info("流程监听类中流程委派任务对象DelegateTask得到的参数：execution:{}",execution);
        Map<String, Object> variables = execution.getVariables();
        logger.info("流程监听类中流程委派任务对象DelegateTask得到的参数：execution的variables:{}",variables);
        String id1 = execution.getId();
        logger.info("流程监听类中流程委派任务对象DelegateTask得到的参数：execution的id:{}",id1);
        String executionId = delegateTask.getExecutionId();
        logger.info("流程监听类中流程委派任务对象DelegateTask得到的参数：executionId:{}",executionId);

        // 动态设置任务审批人
        logger.info("事件名称：eventName:{}",eventName);
        if ("deptAudit".equalsIgnoreCase(taskDefinitionKey)){
            logger.info("流程任务监听类监听到当前任务定义key:{}",taskDefinitionKey);


            delegateTask.setAssignee("25");
            logger.info("部门经理分配的任务执行人是：{}",25);
        }
        if ("ceoAudit".equalsIgnoreCase(taskDefinitionKey)){
            logger.info("流程任务监听类监听到当前任务定义key:{}",taskDefinitionKey);
            delegateTask.setAssignee("23");
        }
        if ("modifyApply".equalsIgnoreCase(taskDefinitionKey)){
            logger.info("流程任务监听类监听到当前任务定义key:{}",taskDefinitionKey);
        }
        if ("tripAudit".equalsIgnoreCase(taskDefinitionKey)){
            logger.info("流程任务监听类监听到当前任务定义key:{}",taskDefinitionKey);
        }

    }
}
